﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

using EFong.ServicesInterface;
using EFong.Contract;
using UIShare;

namespace EFong.View.Proxy
{
    public class FinanceServiceProxy : ClientBase<IFinanceService>, IFinanceService
    {
        #region Constructor

        public FinanceServiceProxy(string endpointConfigurationName) : 
            base(endpointConfigurationName)
        { 
        }

        public FinanceServiceProxy(InstanceContext callbackInstance) :
            base(callbackInstance)
        {
        }

        public FinanceServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName) :
            base(callbackInstance, endpointConfigurationName)
        {
        }

        public FinanceServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public FinanceServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public FinanceServiceProxy(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) :
            base(callbackInstance, binding, remoteAddress)
        {
        }

        public FinanceServiceProxy(Binding binding, EndpointAddress remoteAddress) :
            base( binding, remoteAddress)
        {
        }

        private static FinanceServiceProxy _instance;
        public static FinanceServiceProxy GetInstance() 
        {
            if (_instance == null || _instance.State == CommunicationState.Faulted)
            {
                string url = string.Format("Http://{0}:{1}/{2}", ProxyMgt.IP, ProxyMgt.Port, "FinanceService");

                EndpointAddress epa = new EndpointAddress(url);

                WSHttpBinding binding = new WSHttpBinding();
                //binding.ReliableSession.Enabled = true;
                binding.Security.Mode = SecurityMode.None;
                binding.SendTimeout = new TimeSpan(0, 1, 30);
                binding.MaxReceivedMessageSize = 65536000;
                binding.ReaderQuotas.MaxStringContentLength = 65536000;

                _instance = new FinanceServiceProxy(binding, epa);
            }
            return _instance;
        }

        #endregion

        #region IFinanceService 成员

        public ServiceResult<decimal> CustomerSettle(CustomerSettlement customerSettlement)
        {
            var result = base.Channel.CustomerSettle(customerSettlement);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Order>> GetOrdersByCustomer(Contract.Customer customer)
        {
            var result = base.Channel.GetOrdersByCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateVendorSettlementList(List<Contract.VendorSettlement> vendorSettlementList)
        {
            var result = base.Channel.UpdateVendorSettlementList(vendorSettlementList);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Order>> GetVendorOrders()
        {
            var result = base.Channel.GetVendorOrders();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.VendorSettlement>> GetVendorSettlementByDate(DateTime startDate, DateTime endDate) 
        {
            var result = base.Channel.GetVendorSettlementByDate(startDate,endDate);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult Transfer(AccountTransfer accountTransfer)
        {
            var result = base.Channel.Transfer(accountTransfer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult Payment(OtherPayment otherPayment)
        {
            var result = base.Channel.Payment(otherPayment);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.TourPartySettlement>> GetPendingPayTouringParty()
        {
            var result = base.Channel.GetPendingPayTouringParty();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult TourPartyVendorSettlement(Contract.TourPartySettlement settlement, List<Contract.VendorSettlement> payedList, bool isComplete) 
        {
            var result = base.Channel.TourPartyVendorSettlement(settlement, payedList, isComplete);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult VendorSettle(VendorOtherSettlement vendorSettlement)
        {
            var result = base.Channel.VendorSettle(vendorSettlement);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.飞机票支付单>> GetFlightOrder()
        {
            var result = base.Channel.GetFlightOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.火车票支付单>> GetTicketOrder()
        {
            var result = base.Channel.GetTicketOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.酒店支付单>> GetHotelOrder()
        {
            var result = base.Channel.GetHotelOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.其他支付单>> GetOtherOrder()
        {
            var result = base.Channel.GetOtherOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.自组团支付单>> GetTPOrder()
        {
            var result = base.Channel.GetTPOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.Order>> GetHotelDiscountVendorOrders()
        {
            var result = base.Channel.GetHotelDiscountVendorOrders();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<decimal> GetCash()
        {
            var result = base.Channel.GetCash();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult SetCash(decimal cash)
        {
            var result = base.Channel.SetCash(cash);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        #endregion
    }
}
